package com.mall.product.service;

import com.mall.common.core.domain.Ztree;
import com.mall.common.core.text.Convert;
import com.mall.common.utils.DateUtils;
import com.mall.product.domain.biz.ProCategoryBiz;
import com.mall.product.domain.entity.ProCategory;
import com.mall.product.mapper.ProCategoryMapper;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品分类Service业务层处理
 *
 * @author zhonglin
 * @date 2020-04-28
 */
@Service
public class ProCategoryService {

  @Autowired
  private ProCategoryMapper proCategoryMapper;

  /**
   * 查询商品分类
   *
   * @param categoryId 商品分类ID
   *
   * @return 商品分类
   */
  public ProCategory selectProCategoryById(Long categoryId) {

    return proCategoryMapper.selectProCategoryById(categoryId);
  }

  /**
   * 查询商品分类列表
   *
   * @param proCategory 商品分类
   *
   * @return 商品分类
   */
  public List<ProCategory> selectProCategoryList(ProCategory proCategory) {

    return proCategoryMapper.selectProCategoryList(proCategory);
  }

  /**
   * 新增商品分类
   *
   * @param proCategory 商品分类
   *
   * @return 结果
   */
  public int insertProCategory(ProCategory proCategory) {

    proCategory.setCreateTime(DateUtils.getNowDate());
    return proCategoryMapper.insertProCategory(proCategory);
  }

  /**
   * 修改商品分类
   *
   * @param proCategory 商品分类
   *
   * @return 结果
   */
  public int updateProCategory(ProCategory proCategory) {

    proCategory.setUpdateTime(DateUtils.getNowDate());
    return proCategoryMapper.updateProCategory(proCategory);
  }

  /**
   * 删除商品分类对象
   *
   * @param ids 需要删除的数据ID
   *
   * @return 结果
   */
  public int deleteProCategoryByIds(String ids) {

    return proCategoryMapper.deleteProCategoryByIds(Convert.toStrArray(ids));
  }

  /**
   * 删除商品分类信息
   *
   * @param categoryId 商品分类ID
   *
   * @return 结果
   */
  public int deleteProCategoryById(Long categoryId) {

    return proCategoryMapper.deleteProCategoryById(categoryId);
  }

  /**
   * 查询商品分类树列表
   *
   * @return 所有商品分类信息
   */
  public List<Ztree> selectProCategoryTree() {

    List<ProCategory> proCategoryList = proCategoryMapper.selectProCategoryList(new ProCategory());
    List<Ztree>       ztrees          = new ArrayList<Ztree>();
    for (ProCategory proCategory : proCategoryList) {
      Ztree ztree = new Ztree();
      ztree.setId(proCategory.getCategoryId());
      ztree.setpId(proCategory.getParentId());
      ztree.setName(proCategory.getCategoryName());
      ztree.setTitle(proCategory.getCategoryName());
      ztrees.add(ztree);
    }
    return ztrees;
  }


  public ProCategoryBiz findProCategoryTree() {

    ProCategory proCategory = new ProCategory();
    proCategory.setParentId(0L);
    proCategory.setStatus("0");

    List<ProCategory> categorys = proCategoryMapper.selectProCategoryList(proCategory);

    ProCategoryBiz biz = new ProCategoryBiz();
    List<ProCategoryBiz> categoryList = new ArrayList<>();
    for (ProCategory oneCategory : categorys) {

      ProCategoryBiz oneCategoryBiz = new ProCategoryBiz();
      BeanUtils.copyProperties(oneCategory, oneCategoryBiz);

      proCategory.setParentId(oneCategory.getCategoryId());
      List<ProCategory> twoCategorys = proCategoryMapper.selectProCategoryList(proCategory);

      List<ProCategoryBiz> oneChildrenList = new ArrayList<>();
      for (ProCategory twoCategory : twoCategorys) {

        ProCategoryBiz twoCategoryBiz = new ProCategoryBiz();
        BeanUtils.copyProperties(twoCategory, twoCategoryBiz);

        proCategory.setParentId(twoCategory.getCategoryId());
        List<ProCategory> threeCategorys = proCategoryMapper.selectProCategoryList(proCategory);

        List<ProCategoryBiz> twoChildrenList = new ArrayList<>();
        for (ProCategory threeCategory : threeCategorys) {

          ProCategoryBiz threeCategoryBiz = new ProCategoryBiz();
          BeanUtils.copyProperties(threeCategory, threeCategoryBiz);

          twoChildrenList.add(threeCategoryBiz);
        }

        twoCategoryBiz.setChildren(twoChildrenList);

        oneChildrenList.add(twoCategoryBiz);
      }

      oneCategoryBiz.setChildren(oneChildrenList);

      categoryList.add(oneCategoryBiz);
    }
    biz.setChildren(categoryList);

    return biz;
  }

}
